<template>
  <div class="semester">
    <!--    第一个卡片-->
    <el-card>
      <div class="desc-box">
        <div class="title">学期列表</div>
        <div class="desc">您可以对学期列表进行增删改查操作！</div>
      </div>
      <div class="use-box">
        <div>
          <el-button class="add-button" type="primary" @click="openDialogComponent(0)">添加学期</el-button>
        </div>
      </div>
    </el-card>
    <!--    第二个卡片-->
    <el-card class="second-card">
      <el-table
        ref="table" border
        max-height="530px"
        v-loading="loading"
        element-loading-text="拼命加载中......"
        :data="semesterList"
        :cell-style="{textAlign:'center'}"
        :header-cell-style="{textAlign:'center'}"
        :header-cell-class-name="setHeaderRowClassName"
      >
        <el-table-column type="index" label="序号" fixed></el-table-column>
        <el-table-column prop="Title" label="学期名称"></el-table-column>
        <el-table-column prop="CreationTime" label="创建时间" :formatter="dateFormat"></el-table-column>
        <el-table-column label="启停状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.Status" :active-value="1"
                       :inactive-value="0" @change="statusChange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="openDialogComponent(scope.row.ID)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteCurriculumInfo(scope.row.ID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页-->
      <pagination :child-msg="queryInfo" @callFather="callFather"></pagination>
    </el-card>

    <!--  添加和编辑的对话框-->
    <add-and-edit-dialog
      v-if="isShowDialogComponent"
      :is-show-dialog-component="isShowDialogComponent"
      :title="title"
      :judge-id="judgeId"
      @handleClose="handleClose"
    ></add-and-edit-dialog>
  </div>
</template>

<script>
//引入封装的接口的类
import Semester from '../../model/semester'
//引入添加编辑对话框组件
import AddAndEditDialog from './childComps/AddAndEditDialog'
//引入分页的组件
import Pagination from '../../components/base/pagination/Pagination'
//导入时间按戳的moment
import moment from 'moment'

export default {
  name: 'Semester',
  components: {
    AddAndEditDialog,
    Pagination
  },
  data () {
    return {
      loading: false,
      title: '',
      isShowDialogComponent: false,
      judgeId: 0,
      //学期列表信息
      semesterList: [],
      //获取列表需要传入的参数
      queryInfo: {
        Page: 1,
        Limit: 10,
        total: 0
      },
      statusChangeForm: { id: '' }
    }
  },
  created () {
    this.getSemesterList()
  },
  methods: {
    //时间戳
    dateFormat (row, column) {
      let date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    //设置绑定学科的表头样式
    setHeaderRowClassName ({ rowIndex }) {
      if (rowIndex === 0) {
        return 'setHeaderSubjectColumnName'
      }
    },
    handleClose (payload) {
      if (payload==='submitButton') {
        this.getSemesterList()
      }
      this.isShowDialogComponent = false
    },
    //分页的回调
    callFather (value) {
      if (value.currentPage) this.queryInfo.Page = value.currentPage
      if (value.pageSize) this.queryInfo.Limit = value.pageSize
      this.getSemesterList()
    },
    //打开添加、编辑话框
    openDialogComponent (id) {
      console.log(id)
      this.title = id === 0 ? '添加学期' : '编辑学期'
      this.judgeId = id === 0 ? 0 : id
      this.isShowDialogComponent = true
    },
    //获取基础课程详情列表
    async getSemesterList () {
      try {
        this.loading = true
        const res = await Semester.PageSearch(this.queryInfo)
        this.semesterList = res.ListData
        this.queryInfo.total = res.Total
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    //删除操作
    async deleteCurriculumInfo (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该基础课程详情, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除该基础课程详情的操作')
      } else {
        await Semester.Delete({ ID: id })
        this.$message.success('成功删除该基础课程详情')
        await this.getSemesterList()
      }
    },
    //修改启停状态
    async statusChange (rowInfo) {
      console.log(rowInfo)
      this.statusChangeForm.id = rowInfo.ID
      await Semester.UpdateStatus(this.statusChangeForm)
      await this.getSemesterList()
      this.$message.success('成功修改基础课程详情的启停状态')
    }
  }

}
</script>

<style scoped>
@import "../../../src/assets/style/realize/tableStyle.css";
.semester {
  background-color: #eee;
}
.el-card {
  box-shadow: none;
  margin-top: 10px;
}
.el-pagination {
  position: absolute;
  right: 20px;
  bottom: 10px;
}
.second-card {
  padding-bottom: 50px;
  position: relative;
}
/*给表头一行加背景颜色*/
/deep/ .setHeaderSubjectColumnName {
  background-color: #E6E6FA !important;
  opacity: 0.7 !important;
}
/*描述文本域明显一些的样式，设置高度*/
/deep/ .el-textarea__inner {
  height: 150px;
}
</style>


